package com.smartkey.utils;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.telephony.SmsManager;
import android.widget.Toast;

import com.smartkey.sharedpreferences.SharePreferencesUtil;

public class SystemAppLauncher {

	private Context context;
	private static final SystemAppLauncher INSTANCE = new SystemAppLauncher();

	public static final SystemAppLauncher getInstance() {
		return INSTANCE;
	}

	private SystemAppLauncher() {
	}

	public void init(Context context) {
		this.context = context;
	}

	/**
	 * 打开相机
	 */
	public void launchCamera() {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);

		// Intent intent2 = new Intent();
		// intent2.setClass(context, TakePhotoActivity.class);
		// intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// context.startActivity(intent2);
	}

	/**
	 * 打开摄像
	 */
	public void launchVedio() {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 80); // 设置为低质量
		context.startActivity(intent);
		// startActivityForResult(intent2, REQUEST_CAPTURE_VIDEO);
	}

	/**
	 * 打开录音
	 */
	public void launchRecoding() {
		// 调用Android自带的音频录制应用
		Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	/**
	 * 打开手电筒
	 */
	public void launchFlashlight() {
		SharePreferencesUtil preferencesUtil = new SharePreferencesUtil(context);
		boolean flashLightStatus = preferencesUtil.getFlashLightTag();
		if (ModelUtils.isXIAOMI()) {
			if (flashLightStatus) {
				FlashLightUtil.getInstance(context).turnOffFlashLightXIAOMI();
			} else {
				FlashLightUtil.getInstance(context).turnOnFlashLightXIAOMI();
			}
		} else {
			if (flashLightStatus) {
				FlashLightUtil.getInstance(context).turnOffFlashLight();
			} else {
				FlashLightUtil.getInstance(context).turnOnFlashLight();
			}
		}
		preferencesUtil.setFlashLightTag(!flashLightStatus);
	}

	/**
	 * 截屏
	 */
	public void launchScreenShot() {
		// 小米手机截屏方式
		if (ModelUtils.isXIAOMI()) {
			ScreenShotUtil.screenShotXIAOMI(context);
		} else { // 其他手机
			ScreenShotUtil.screentShotByRoot(context);
		}
	}

	/**
	 * 模拟home键
	 */
	public void launcherHome() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 注意
		intent.addCategory(Intent.CATEGORY_HOME);
		context.startActivity(intent);
	}

	/**
	 * 清除内存
	 */
	public void launchClearMemory() {
		// 小米手机清理内存
		if (ModelUtils.isXIAOMI()) {
			Intent localIntent = new Intent(
					"com.android.systemui.taskmanager.Clear");
			context.sendBroadcast(localIntent);
		} else {
			MemoryUtils.clearMemory(context);
		}
		Toast.makeText(context, "已清理到最佳状态", Toast.LENGTH_SHORT).show();
	}

	/**
	 * 打电话
	 */
	public void launchDial() {
		SharePreferencesUtil sharePreferencesUtil = new SharePreferencesUtil(context);
		String phoneNumber = sharePreferencesUtil.getCallNumber();
		
		Intent intent = new Intent("android.intent.action.CALL",
				Uri.parse("tel:" + phoneNumber));
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	/**
	 * 发短信
	 */
	public void launchSendMessage() {
		SharePreferencesUtil sharePreferencesUtil = new SharePreferencesUtil(context);
		String phoneNumber = sharePreferencesUtil.getSMSNumber();
		String messageContent = sharePreferencesUtil.getSMSMsg();
		
		//以下是直接发送短信
		SmsManager manager = SmsManager.getDefault();
		ArrayList<String> list = manager.divideMessage(messageContent); // 因为一条短信有字数限制，因此要将长短信拆分
		for (String text : list) {
			manager.sendTextMessage(phoneNumber, null, text, null, null);
		}
		
		//以下是先弹出短信界面，后再发送
//		Uri smsToUri = Uri.parse("smsto:"+phoneNumber);  
//		Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);  
//		intent.putExtra("sms_body", messageContent); 
//		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
//		context.startActivity(intent);  
	}

	/**
	 * 旋屏
	 */
	public void launchRotatingScreen() {
		ScreenRotationUtils.changeRotationStatus(context);
	}

}
